package com.base.cn.platform.os.controller.web.classes.personal.topic;

import com.base.cn.platform.os.common.WebBaseController;
import com.base.cn.platform.os.common.utils.StringUtils;
import com.base.cn.platform.os.service.course.classes.ClassesService;
import com.base.cn.platform.os.service.course.classes.member.ClassesMemberService;
import com.base.cn.platform.os.service.course.classes.topic.ClassesTopicService;
import com.base.cn.platform.os.service.course.classes.topic.type.ClassesTopicTypeService;
import com.base.cn.platform.os.service.manage.user.user.CusUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 我的话题
 *
 * @author SK
 * @since 2018-05-28
 */
@Controller
public class WebPersonalTopicController extends WebBaseController {

    @Autowired
    private ClassesMemberService classesMemberService;
    @Autowired
    private ClassesTopicService classesTopicService;
    @Autowired
    private CusUserService cusUserService;
    @Autowired
    private ClassesService classesService;
    @Autowired
    private ClassesTopicTypeService classesTopicTypeService;

    /**
     * 我的话题首页
     */
    @RequestMapping("/web/uc/classes/personal/topic/index")
    public String index(HttpServletRequest request,
                        Model model) {
        BigDecimal userId = webMobileLoginUtil.getLoginUserId(request);
        Map<String, Object> cusUser = cusUserService.userInfo(userId, false, false, false, false, false, false, false);
        model.addAttribute("cusUser", cusUser);
        Map<String, Object> statistics = classesMemberService.classesMemberStatistics(userId);
        model.addAttribute("statistics", statistics);
        //话题分类统计
        List<Map<String, Object>> classesTypeNum = classesTopicTypeService.getClassesTypeNum(null, userId);
        model.addAttribute("topicTypeNum",classesTypeNum);
        return resultPage("/classes/personal/topic/personal-topic");
    }

    /**
     * 我的话题
     *
     * @param currentPage 第N页
     * @param which       1.全部话题 2.发表的话题 3.参与的话题，4.转发的话题
     * @return 我的话题
     */
    @RequestMapping("/web/classes/personal/topic/ajaxPersonTopicList")
    public String ajaxPersonTopicList(Model model,
                                      HttpServletRequest request,
                                      @RequestParam(value = "currentPage", required = false, defaultValue = "1") Integer currentPage,
                                      @RequestParam(value = "which", required = false, defaultValue = "1") Integer which) {
        BigDecimal userId = webMobileLoginUtil.getLoginUserId(request);
        Map<String, Object> topicMap = classesTopicService.findClassesPersonalTopicPage(userId, currentPage, null, which, true, false, false);
        if (StringUtils.isNotEmpty(topicMap)){
            List<Map<String, Object>> topiclist = (List<Map<String, Object>>) topicMap.get("list");
            topiclist.forEach(e->{
                String context = (String) e.get("content");
                e.put("content",context.replaceAll("<img.*?>", ""));
            });
        }
        model.addAttribute("dataMap", topicMap);
        return resultPage("/classes/topic/ajax-topic-list");
    }
}
